<?php
class CalcApp extends MemberbaseApp{
    var $calc_mod;
    function __construct()
    {
        $this->CalcApp();
    }
    function CalcApp()
    {
        parent::__construct();
        $this->calc_mod=&m('calc');
		//$this->check_powers(USER_ADMIN);
    }
    function manage(){
         $this->check_powers(USER_ADMIN);
        $calc_list=$this->calc_mod->find();
        $this->assign('calcs',$calc_list);
        $this->display('calc.manage.html');
    }
    function index()
    {
       $this->check_powers(array(USER_MANAGER,USER_MERCHANDISER));
        $calc_list=$this->calc_mod->find();
        foreach($calc_list as $c=>$v){
            $calc_list[$c]['para']=unserialize($v['para']);
            $calc_list[$c]['gongshi']=str_replace('#', 'input', $v['gongshi']);
        }
		
		 $this->_curlocal("报价公式");

        $this->assign('calcs',$calc_list);
        $this->display('calc.index.html');
    }
    function show(){
        $this->check_powers(array(USER_MANAGER,USER_MERCHANDISER));
        $id=$_GET['id']?intval($_GET['id']):'0';
        $calc=$calc_list=$this->calc_mod->get($id);
        !$calc && $this->show_warning('报价公式不存在');
        $calc['para']=unserialize($calc['para']);
        $calc['gongshi']=str_replace('#', 'input', $calc['gongshi']);
        $this->assign('calc',$calc);
        $this->display('calc.show.html');
    }
    function add(){
        $this->check_powers(USER_ADMIN);
        $id=$_GET['id']?intval($_GET['id']):'0';
        if($id){
            $calc=$calc_list=$this->calc_mod->get($id);
        }
        if(IS_POST){
            $data=$_POST['info'];
            foreach($_POST['para'] as $k=>$v){
                if($v){
                    $para[$k]=$v;
                }
            }
            $data['para']=serialize($para);
            if($id){
                $this->calc_mod->edit($id,$data);
                $this->show_message('编辑成功','返回首页','index.php?app=calc&act=manage');
                return;
            }
            else {
                $this->calc_mod->add($data);
                $this->show_message('添加成功','返回首页','index.php?app=calc&act=manage');
                return;
            }
            
        }
        else {
            $calc['para']=unserialize($calc['para']);
            $this->assign('calc',$calc);
        }
        
        $this->display('calc.form.html');
    }
    function del(){
        $this->check_powers(USER_ADMIN);
        $id=$_GET['id']?intval($_GET['id']):'0';
        $calc=$calc_list=$this->calc_mod->drop($id);
        $this->show_message('删除成功','返回首页','index.php?app=calc&act=manage');
    }
}

?>
